﻿
Chenxin.UI.CommunityBuildingMgr = Chenxin.extend(Chenxin.UI.PageCommon, {
    actionUrl: 'service/CommunityBuildingMgrMore.ashx',
    pageSize: 4,
    createGrid: function() {
        return new Chenxin.UI.Pagination({
            actionUrl: this.actionUrl + '?op=getlist',
            id: 'communitybuilding',
            renderTo: 'communitybuilding_table',
            pageSize: 100,
            autoLoad: this.autoLoadGrid
        });
    },
    bulkAdd: function() {
        this.createBulkForm();
        this.bulkForm.show();
        this.bulkForm.fitData({ BuildingNameType: '栋', MinNumber: '1', MaxNumber: '1', LengthNumber: '2', MinChar: 'A', MaxChar: 'Z', BuildingName: '*' });
    },
    createBulkForm: function() {
        if (!this.bulkForm) {
            this.bulkForm = new Chenxin.UI.CommunityBuildingInfoBulkForm({
                scope: this,
                saveSucc: function() {
                    this.grid.reload();
                }
            });
        }
    },
    initEvent: function() {
        var _this = this;
        $("#btn_add_building").live("click", function() {
            _this.form.fitData({ BuildingNameType: '栋' });
            _this.add();
        });
        $("#btn_bulkadd_building").live("click", function() {
            _this.bulkAdd();
        });
        $('#goon_addbuilding').click(function() {
            $('#addbuild_msg').jqmHide();
            _this.form.reset();
            _this.add();
        });
        $('#edit_building').click(function() {
            $('#addbuild_msg').jqmHide();
            _this.edit();
        });
        $('.a-communitybuilding-edit').live('click', function() {
            _this.currentId = this.name;
            _this.edit();
        });

        $('.a-communitybuilding-del').live('click', function() {
            _this.currentId = this.name;
            _this.del();
        });
    },
    createForm: function() {
        return new Chenxin.UI.CommunityBuildingInfoForm({
            scope: this,
            afterGetFormJson: function(json) {
                json.BuildingName += json.BuildingNameType;
                json.CommunityId = this.communityId;
                json.RoomRuleRemark = $("#" + this.form.id + " :radio[name='RoomRule'][checked='true']").attr('title');
                delete json.BuildingNameType;
                return json;
            },
            beforeFitData: function(model) {
                if (!model.BuildingName) {
                    return;
                }
                if (model.BuildingName.indexOf('号') == model.BuildingName.length - 1 || model.BuildingName.indexOf('栋') == model.BuildingName.length - 1 || model.BuildingName.indexOf('幢') == model.BuildingName.length - 1) {
                    var name = model.BuildingName;
                    model.BuildingName = name.substring(0, name.length - 1);
                    model.BuildingNameType = name.substring(name.length - 1, name.length)
                }
            },
            saveSucc: function(data, msg) {
                if ($('#' + this.grid.renderTo).length) {
                    this.grid.reload();
                }
                this.currentId = data.BuildingId;
                $('#addbuild_msg').jqmShow();
            }
        });
    },
    initComponent: function() {
        Chenxin.UI.CommunityBuildingMgr.superclass.initComponent.call(this);
    }
});

Chenxin.UI.CommunityBuildingInfoBulkForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/CommunityBuildingMgr.ashx',
    title: '批量添加',
    id: 'communityBuildingBulkWin',
    renderTo: 'jqm_communityBuildingBulk',
    initFormValidator: function() {
        var _this = this;

        //楼栋层数验证
        $("#communitybuildingbulk_Floor").formValidator({
            validatorgroup: this.id,
            onshow: "请输入楼栋层数!", onfocus: "请输入楼栋层数!", oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "楼栋层数不能为空!" }, onerror: "楼栋层数不能为空!"
        }).functionValidator({ //数字验证
            fun: function(val, elem) { return val.isNum(); }, onerror: "请输入数字"
        });

        //单元数验证
        $("#communitybuildingbulk_UnitNum").formValidator({
            validatorgroup: this.id,
            onshow: "请输入单元数!", onfocus: "请输入单元数!", oncorrect: " "
        }).functionValidator({ //数字验证
            fun: function(val, elem) { return val.isNum(); }, onerror: "请输入数字"
        });

    },
    afterGetFormJson: function(model) {
        this.bulkForm.additionalParam = {};
        if (model.setting == 'number') {
            this.bulkForm.additionalParam.MinNumber = model.MinNumber;
            this.bulkForm.additionalParam.MaxNumber = model.MaxNumber;
            this.bulkForm.additionalParam.LengthNumber = model.LengthNumber;
        } else {
            this.bulkForm.additionalParam.MinChar = model.MinChar;
            this.bulkForm.additionalParam.MaxChar = model.MaxChar;
            this.bulkForm.additionalParam.LengthNumber = model.LengthNumber;
        }
        this.bulkForm.additionalParam.BuildingNameType = model.BuildingNameType;
        this.bulkForm.additionalParam.setting = model.setting;

        model.CommunityId = this.communityId;
        model.RoomRuleRemark = $("#" + this.bulkForm.id + " :radio[name='RoomRule'][checked='true']").attr('title');


        delete model.setting;
        delete model.MinChar;
        delete model.MaxChar;
        delete model.MinNumber;
        delete model.MaxNumber;
        delete model.LengthNumber;
        delete model.BuildingNameType;
        this.bulkForm.op = 'bulkadd';
    },
    customerFormVal: function(json) {
        try {
            if (json.BuildingName.indexOf('*') == -1) {
                alert('楼栋名称必须包含"*"');
                return false;
            } else if (json.BuildingName.lastIndexOf('*') != json.BuildingName.indexOf('*')) {
                alert('楼栋名称只能包含一个"*"');
                return false;
            }
            var model = this.additionalParam;
            if (model.setting == 'number') {
                var num = /^[0-9]{1,3}$/;
                if (!num.test(model.MinNumber) || !num.test(model.MaxNumber) || !num.test(model.LengthNumber)) {
                    alert('配置项必须为数字,且位数不许小于3位！');
                    return false;
                }
                if (parseInt(model.LengthNumber) > 3 || parseInt(model.LengthNumber) < 1) {
                    alert('长度不能大于3和小于1!');
                    return false;
                }
            } else {
                var ch = /^[A-Z]{1}$/;
                if (!ch.test(model.MinChar) || !ch.test(model.MaxChar)) {
                    alert('配置项必须为大写字母A-Z！');
                    return false;
                }
            }
            return true;
        } catch (e) {
            alert('配置项设置错误！');
            return false;
        }
    },
    createForm: function() {
        return Chenxin.UI.FormCommon.communityBuildingInfoBulkForm('communitybuildingbulk');
    },
    initEvent: function() {
        var _this = this;
        $('#communitybuildingbulk_Save').live('click', function() {
            _this.save();
        });
        $('#communitybuildingbulk_Cancle').live('click', function() {
            _this.hide();
        });
    },
    createComboList: function() {
        var nameType = new Chenxin.UI.Combo({
            containerId: '#communitybuildingbulk_BuildingNameType',
            name: 'BuildingNameType',
            addPleaseSelected: false,
            items: comboCommon.buildingNameType,
            required: true
        });
        var unitType = new Chenxin.UI.Combo({
            containerId: '#communitybuildingbulk_UnitType',
            name: 'UnitType',
            items: comboCommon.buildingUnitType
        });
        return [nameType, unitType];
    },
    initComponent: function() {
        Chenxin.UI.CommunityBuildingInfoBulkForm.superclass.initComponent.call(this);
    }
});

Chenxin.UI.CommunityBuildingInfoForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: 'service/CommunityBuildingMgr.ashx',
    id: 'communityBuildingWin',
    renderTo: 'jqm_communityBuilding',
    show: function() {
        if (this.op == 'add') {
            this.setTitle('添加楼栋');
        } else if (this.op == 'edit') {
            this.setTitle('编辑楼栋');
        }
        return Chenxin.UI.CommunityBuildingInfoForm.superclass.show.call(this);
    },
    initFormValidator: function() {
        var _this = this;

        //楼栋名称验证
        $("#txtBuildingName").formValidator({
            validatorgroup: this.id,
            onshow: "请输入楼栋名称!", onfocus: "请输入楼栋名称!", oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "楼栋名称不能为空!" }, onerror: "楼栋名称不能为空!"
        }).functionValidator({ //数字验证
            fun: function(val, elem) {
                if (_this.op == 'edit' && _this.scope.currentModel.BuildingName == val) {
                    return true;
                }
                var _check = false;
                Chenxin.ajax({
                    url: _this.actionUrl,
                    data: { op: 'validate', communityid: _this.scope.communityId, txtBuildingName: val, clientid: elem.id },
                    onSuccess: function() {
                        _check = true;
                    },
                    async: false,
                    onFail: function() {
                        _check = false;
                    }
                });
                return _check;
            },
            onerror: "楼盘名称已存在"
        });

        //楼栋层数验证
        $("#txtFloor").formValidator({
            validatorgroup: this.id,
            onshow: "请输入楼栋层数!", onfocus: "请输入楼栋层数!", oncorrect: " "
        }).inputValidator({ //非空验证
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "楼栋层数不能为空!" }, onerror: "楼栋层数不能为空!"
        }).functionValidator({ //数字验证
            fun: function(val, elem) { return val.isNum(); }, onerror: "请输入数字"
        });

        //单元数验证
        $("#txtUnitNum").formValidator({
            validatorgroup: this.id,
            onshow: "请输入单元数!", onfocus: "请输入单元数!", oncorrect: " "
        }).functionValidator({ //数字验证
            fun: function(val, elem) { return val.isNum(); }, onerror: "请输入数字"
        });
    },
    createForm: function() {
        return Chenxin.UI.FormCommon.communityBuildingInfoForm('communitybuilding');
    },
    initEvent: function() {
        var _this = this;
        $('#communitybuilding_Save').live('click', function() {
            _this.save();
        });
        $('#communitybuilding_Cancle').live('click', function() {
            _this.hide();
        });
    },
    createComboList: function() {

        var nameType = new Chenxin.UI.Combo({
            containerId: '#BuildingNameType',
            name: 'BuildingNameType',
            addPleaseSelected: false,
            items: comboCommon.buildingNameType,
            required: true
        });
        var unitType = new Chenxin.UI.Combo({
            containerId: '#UnitType',
            name: 'UnitType',
            items: comboCommon.buildingUnitType
        });

        return [nameType, unitType];
    },
    initComponent: function() {
        Chenxin.UI.CommunityBuildingInfoForm.superclass.initComponent.call(this);
    }
});

/**
*@description 楼栋DIV
*@method 提交按钮的处理方法
*/
Chenxin.UI.FormCommon.communityBuildingInfoForm = function(comboIdPrefix) {
    return '<table class="tbl_f"><tr><th><span class="isfilled">*</span>楼栋名称：</th><td><input type="hidden" name="BuildingId"/><input type="text" style="width:120px;" value="" class="inputbox_n fl" id="txtBuildingName" name="BuildingName"/><div id="txtBuildingNameTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;"></div></td></tr><tr><th><span class="isfilled">*</span>类型：</th><td><div id="BuildingNameType" style="z-index:902; margin-right: 5px;" class="SelectWrapSingle validator_build"></div></td></tr><tr><th><span class="isfilled">*</span>总层数：</th><td><input type="text" style="width:120px;" value="" class="inputbox_n fl" id="txtFloor" name="Floor" /><div style="float:left;height: 26px; line-height:26px;padding-left:6px;">层</div><div id="txtFloorTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;">请填写层数</div></td></tr><tr><th><span class="isfilled">*</span>单元数：</th><td><input type="text" style="width: 120px;" value="" class="inputbox_n fl" id="txtUnitNum" name="UnitNum" /><div id="txtUnitNumTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;"></div></td></tr><tr><th>单元类型：</th><td> <div id="UnitType" style="z-index:902; margin-right: 5px;" class="SelectWrapSingle validator_build"></div></td></tr><tr><th>楼栋规则：</th><td><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(数字2位) [例：601，1804]" value="^[0-9]{3,4}$" checked="checked" class="lineh15"/>楼层(1-2位)+房间号(数字2位) [例：601，1804]<br /><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(字母1位) [例：6A，18D]" value="^[0-9]{1,2}[a-zA-Z]{1}$"  class="lineh15"/>楼层(1-2位)+房间号(字母1位) [例：6A，18D]<br /><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(字母1位/数字2位) [例：601，18D]" value="^[0-9]{1,2}([a-zA-Z]{1}|[0-9]{2})$"  class="lineh15"/>楼层(1-2位)+房间号(字母1位/数字2位) [例：601，18D]<br /><input type="radio" name="RoomRule" title="大写字母,数字任意组合 [例：601，18D]" value="^[A-Z0-9]*$"  class="lineh15"/>大写字母,数字任意组合 [例：601，18D]<br /><input type="radio" name="RoomRule" title="独(全)幢(无房间号)" value="*"  class="lineh15"/>独(全)幢(无房间号)<br /><input type="radio" name="RoomRule" title="任意房间号" value="."  class="lineh15"/>任意房间号<br /></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><input type="hidden" name="CommunityId"/><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></table>'
};

/**
*@description 楼栋DIV
*@method 提交按钮的处理方法
*/
Chenxin.UI.FormCommon.communityBuildingInfoBulkForm = function(comboIdPrefix) {
    return '<table class="tbl_f"><tr><th>楼栋名称：</th><td><input type="text" class="inputbox_n" name="BuildingName" style="width:120px" value="*"/></td></tr><tr><th></th><td>楼栋名称通配符设置：</td></tr><tr><th></th><td><input type="radio" name="setting" class="lineh15" checked="checked" value="number"/> 从 <input type="text" class="inputbox_n"  style="width:30px" name="MinNumber" value="1" /> 到 <input type="text" class="inputbox_n" style="width:30px" name="MaxNumber" value="1" /> 长度 <input type="text" class="inputbox_n" name="LengthNumber" value="2"  style="width:30px"/> (1-3)</td></tr><tr><th></th><td><input type="radio" name="setting" class="lineh15" value="char"/> 从 <input type="text" class="inputbox_n"  style="width:30px" name="MinChar" value="A" /> 到 <input type="text" class="inputbox_n" style="width:30px" name="MaxChar" value="Z" /></td></tr><tr><th><span class="isfilled">*</span>类型：</th><td><div id="' + comboIdPrefix + '_BuildingNameType" style="z-index:902; margin-right: 5px;" class="SelectWrapSingle validator_build"></div></td></tr><tr><th><span class="isfilled">*</span>总层数：</th><td><input type="text" style="width:120px;" value="" class="inputbox_n fl" id="' + comboIdPrefix + '_Floor" name="Floor" /><div style="float:left;height: 26px; line-height:26px;padding-left:6px;">层</div><div id="' + comboIdPrefix + '_FloorTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;">请填写层数</div></td></tr><tr><th><span class="isfilled">*</span>单元数：</th><td><input type="text" style="width: 120px;" value="" class="inputbox_n fl" id="' + comboIdPrefix + '_UnitNum" name="UnitNum" /><div id="' + comboIdPrefix + '_UnitNumTip" style="float:left; margin-left: 5px; width: 120px; height: 26px; line-height: 26px; text-align: left;"></div></td></tr><tr><th>单元类型：</th><td> <div id="' + comboIdPrefix + '_UnitType" style="z-index:902; margin-right: 5px;" class="SelectWrapSingle validator_build"></div></td></tr><tr><th>楼栋规则：</th><td><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(数字2位) [例：601，1804]" value="^[0-9]{3,4}$" checked="checked" class="lineh15"/>楼层(1-2位)+房间号(数字2位) [例：601，1804]<br /><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(字母1位) [例：6A，18D]" value="^[0-9]{1,2}[a-zA-Z]{1}$"  class="lineh15"/>楼层(1-2位)+房间号(字母1位) [例：6A，18D]<br /><input type="radio" name="RoomRule" title="楼层(1-2位)+房间号(字母1位/数字2位) [例：601，18D]" value="^[0-9]{1,2}([a-zA-Z]{1}|[0-9]{2})$"  class="lineh15"/>楼层(1-2位)+房间号(字母1位/数字2位) [例：601，18D]<br /><input type="radio" name="RoomRule" title="大写字母,数字任意组合 [例：601，18D]" value="^[A-Z0-9]*$"  class="lineh15"/>大写字母,数字任意组合 [例：601，18D]<br /><input type="radio" name="RoomRule" title="独(全)幢(无房间号)" value="*"  class="lineh15"/>独(全)幢(无房间号)<br /><input type="radio" name="RoomRule" title="任意房间号" value="."  class="lineh15"/>任意房间号<br /></td></tr><tr><td colspan="2" align="right"><div style="float:right; margin-right:15px;"><input type="hidden" name="CommunityId"/><a id="' + comboIdPrefix + '_Save" class="btn_n fl" href="#"><span>确定</span></a><a id="' + comboIdPrefix + '_Cancle"  class="btn_n fl" href="#"><span>取消</span></a></div></td></tr></table>'
};